<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>全选和反选</title>
	</head>
	<body>
		<input type="checkbox" name="chek" value="pg" />苹果<br />
		<input type="checkbox" name="chek" value="li" />梨子<br />
		<input type="checkbox" name="chek" value="xj" />香蕉<br />
		<input type="checkbox" name="chek" value="bl" />菠萝<br />
		<input type="button"  id="selALL"value="全选" onclick="selAll();"/>
		<input type="button"  id="fx" value="反选" onclick="fx();"/>
	</body>
	<script>
		function selAll(){
			var checkelt=document.getElementsByName("chek");
			//alert(checkelt.length);
			for(var index=0;index<=checkelt.length-1;index++){
				checkelt[index].checked=true;
			}
		}
		function fx(){
			var checkelt=document.getElementsByName("chek");
			//alert(checkelt.length);
			for(var index=0;index<=checkelt.length-1;index++){
				if(checkelt[index].checked==true){
					checkelt[index].checked=false;
				}else if(checkelt[index].checked==false){
					checkelt[index].checked=true;
				}
				
			}
		}
		
	</script>
</html>
